<?php
include('SimpleImage.php');
ob_start();
include('../include/connect.php');

function getinfo($name,$table,$tablename,$tableid){
	$name = substr($name,1 );
	$select = mysql_query("select * from {$table} where {$tablename} LIKE '%{$name}%'") or die(mysql_error());
	$d=mysql_fetch_array($select);
	if(mysql_num_rows($select)>0)
		$id=$d["{$tableid}"];
	else $id="1";
	return $id;
}
function insert($name,$tablename){
	if($tablename=="theloai")
	{
		if($query=mysql_query("insert into {$tablename} value('','{$name}')") or die())
			return true;
		else 
			return false;
	}
	else 
	{
		if($query=mysql_query("insert into {$tablename} value('','{$name}')") or die(mysql_error()))
			return true;
		else 
			return false;
	}
}
function getvariable($name,$table,$tablename,$tableid){
	if($name==" " or $name == "")
	{
		$value="0";
	}
	else 
	{
		if(getinfo($name,$table,$tablename,$tableid)!="1")
		{	
			$value=getinfo($name,$table,$tablename,$tableid);
		}
		else 
		{
			if(insert($name,$table))
				$value = getinfo($name,$table,$tablename,$tableid);
			else 
				$value = "error";
		}
	}
	return $value;
}
function cut_name($array,$condition){
	$myArray = explode($condition, $array);
	return $myArray;
}
function save_img($url){
	$exp = explode('/',$url);
	$lastIndex = sizeof($exp);
	$ch = curl_init ($url);
	$path="../upload/".str_replace('%','',$exp[$lastIndex-1]);
	$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
	$rawdata=curl_exec ($ch);
	curl_close ($ch);
	
	$fp = fopen($path,'w');
	fwrite($fp, $rawdata); 
	fclose($fp);
	return $path = "upload/".str_replace('%','',$exp[$lastIndex-1]);
}
if(isset($_POST['img']))
{
	$ten=$_POST['ten'];
	$tencheck = mysql_query("select * from phim where tenphim = '{$ten}'") or die("select * from phim where tenphim = '{$ten}'");
	if(mysql_num_rows($tencheck)>1)
		{
			header("location:admin_index.php");
		}
	else 
	{
		$maloai=$_POST['loai'];	
		$post_img = explode('?',$_POST['img']);
		$post_img_thumbnail = explode('?',$_POST['img_thumbnail']);
		$img=save_img($post_img[0]);
		$img_thumbnail=save_img($post_img_thumbnail[0]);	
		$daodien=getvariable($_POST['daodien'],"daodien","tendaodien","madaodien");
		$dienvien=cut_name($_POST['dienvien'],",");
		for($i=0;$i<sizeof($dienvien);$i++)
		{
			$madienvien[$i]=getvariable($dienvien[$i],"dienvien","tendienvien","madienvien");
		}
		$nsx=getvariable($_POST['nsx'],"nhasx","tennsx","mansx");
		$qg=getvariable($_POST['qg'],"quocgia","tenquocgia","maquocgia");
		$tl=cut_name($_POST['tl'],",");
		for($i=0;$i<sizeof($tl);$i++)
		{
			$matl[$i]=getvariable($tl[$i],"theloai","tentheloai","matheloai");
		}
		$time=$_POST['time'];
		if($_POST['quality']!="") $quality=getvariable($_POST['quality'],"chatluong","tenchatluong","machatluong");else $quality=3;
		$sub=getvariable($_POST['sub'],"chuyenngu","tenchuyenngu","machuyenngu");
		$year=$_POST['year'];
		$day1=$_POST['day1'];
		$day2=$_POST['day2'];
		$noidung=$_POST['noidung'];
		if(mysql_query("insert into phim value('','$ten',$nsx,$daodien,$qg,$quality,$sub,'$img','$img_thumbnail','$year','$time','$noidung','$day1','$day2','lienket','maloai','$ten','0')") or die("insert into phim value('','$ten',$nsx,$daodien,$qg,$quality,$sub,'$img','$img_thumbnail','$year','$time','$noidung','$day1','$day2','lienket','{$maloai}','$ten','0')"))
		{
			$phim = mysql_query("select * from phim where tenphim = '{$ten}'");
			$e=mysql_fetch_array($phim);
			for($i=0;$i<sizeof($matl);$i++) 
				mysql_query("insert into cttheloai value({$e['maphim']},{$matl[$i]})") or die("insert into cttheloai value({$e['maphim']},{$matl[$i]})");
			for($i=0;$i<sizeof($madienvien);$i++)
				mysql_query("insert into ctdienvien value({$e['maphim']},{$madienvien[$i]})") or die("insert into ctdienvien value({$e['maphim']},{$madienvien[$i]})");
			$phim= $_POST['phim'];
			$s=0;
			for($i=0;$i<=(sizeof($phim)-1);$i++)
			{
				$tap = cut_name($phim[$i],"+");
				if(strtoupper($tap[0])=="XEM FULL" or $tap[2]!='') $s++;
				$tap[0]=$tap[0].'-'.$s."-".$tap[2];
				mysql_query("insert into phim(tenphim,lienket,tentap) value('$ten','{$tap[1]}','{$tap[0]}')") or die("insert into phim(tenphim,lienket,tentap) value('$ten','{$tap[1]}','{$tap[0]}')");
			}
			//echo header("location:admin_auto_add.php");
		}
		else 
			header('location:admin_auto_add_viphim.php');
	}
}
else header('location:admin_index.php');
?>